package org.budo.netty.servlet.server.handler.servlet.registration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import javax.servlet.ServletSecurityElement;

import org.budo.netty.servlet.server.handler.servlet.BudoServletContext;

/**
 * @author lmw
 */
public class BudoServletRegistration implements ServletRegistration.Dynamic {
    private String className;

    private ServletContext servletContext;

    private Map<String, String> initParameters = new HashMap<String, String>();

    private List<String> urlPatterns = new ArrayList<String>();

    // load-on-startup 元素标记容器是否应该在web应用程序启动的时候就加载这个servlet，(实例化并调用其init()方法)。
    // 它的值必须是一个整数，表示servlet被加载的先后顺序。
    // 如果该元素的值为负数或者没有设置，则容器会当Servlet被请求时再加载。
    // 如果值为正整数或者0时，表示容器在应用启动时就加载并初始化这个servlet，值越小，servlet的优先级越高，就越先被加载。值相同时，容器就会自己选择顺序来加载。
    private Integer loadOnStartup = Integer.MAX_VALUE;

    private String servletName;

    public BudoServletRegistration(String servletName, String className, BudoServletContext servletContext) {
        this.servletName = servletName;
        this.className = className;
        this.servletContext = servletContext;
    }

    @Override
    public String getClassName() {
        return this.className;
    }

    /**
     * @return 在其他servlet中已注册的url
     */
    @Override
    public Set<String> addMapping(String... urlPatterns) {
        this.urlPatterns.addAll(Arrays.asList(urlPatterns));
        return null;
    }

    /**
     * 返回重复的
     */
    @Override
    public Set<String> setInitParameters(Map<String, String> initParameters) {
        this.initParameters.putAll(initParameters);
        return null;
    }

    @Override
    public Collection<String> getMappings() {
        return this.urlPatterns;
    }

    @Override
    public void setLoadOnStartup(int loadOnStartup) {
        this.loadOnStartup = loadOnStartup;
    }

    public Integer getLoadOnStartup() {
        return this.loadOnStartup;
    }

    @Override
    public Map<String, String> getInitParameters() {
        return this.initParameters;
    }

    @Override
    public String getInitParameter(String name) {
        return this.initParameters.get(name);
    }

    public ServletContext getServletContext() {
        return this.servletContext;
    }

    @Override
    public String getName() {
        return this.servletName;
    }

    @Override
    public String getRunAsRole() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean setInitParameter(String name, String value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setAsyncSupported(boolean isAsyncSupported) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<String> setServletSecurity(ServletSecurityElement constraint) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setMultipartConfig(MultipartConfigElement multipartConfig) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setRunAsRole(String roleName) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toString() {
        return super.toString() + ", name=" + this.getName() + ", className=" + this.getClassName() + ", loadOnStartup=" + this.loadOnStartup;
    }
}